package sessionBeans;

/// For the commit
import java.util.ArrayList;
import java.util.Collection;

import entityBeans.User;
import entityBeans.Ability;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import objects.AbilityObject;
import objects.HelpmeetingObject;
import objects.UserObject;

import org.jboss.ejb3.annotation.RemoteBinding;

/**
*
* @author user
*/
@Stateless
//@RemoteBinding(jndiBinding="SearchFacadeJNDI")

public class SearchFacade extends Ability implements SearchFacadeLocal
{
	 @PersistenceContext(unitName = "sw2")
	    private EntityManager em;

	
	/*
	 * this method will search a user's friend 
	 */
	 @Override
	public Collection<UserObject> searchFriend(String keyword) throws BeanException{
		
		if(keyword==null){
			throw new BeanException("Please enter a name to search");
		}else{
			Query q;
			q = em.createQuery("SELECT u FROM User u WHERE u.name = :name");
			q.setParameter("name", keyword);
			
			Collection<UserObject> users = new ArrayList<UserObject>();
			users= ((Collection<UserObject>)q.getResultList());
			
			return users;
		}
		 
	 }
	
	/*
	 *  this method will search a user's ability from able table
	 */
	 @Override
	public Collection<AbilityObject> searchAbility(String keyword)throws BeanException{
		
		 	if(keyword==null){
				throw new BeanException("Please enter a text as an ability name to search");
			}else{
				Query q;
				q = em.createQuery("SELECT a FROM Ability a WHERE a.name = :name");
				q.setParameter("name", keyword);
				
				Collection<AbilityObject> abilities = new ArrayList<AbilityObject>();
				abilities= ((Collection<AbilityObject>)q.getResultList());
				
				return abilities;
			}
	}

}
